package simulation;

public class findPoisonedDuration {
    public int findPoisonedDuration(int[] timeSeries, int duration) {
        int sum = 0;
        for(int i = 0;i < timeSeries.length;){
            int end = timeSeries[i]+duration-1;
            if(i < timeSeries.length-1&& end >= timeSeries[i+1]) {
                sum += timeSeries[i+1]-timeSeries[i];
                end = timeSeries[i+1]+duration-1;
                i++;
            }else{
                sum += end-timeSeries[i]+1;
                i++;
            }

        }
        return sum;
    }
}
